package 反射;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Student{
    //私有属性name
    private String name = "bit";
    //公有属性age
    public int age = 18;
    //不带参数的构造方法
    public Student(){
        System.out.println("Student()");
    }
    //私有
    private Student(String name,int age) {
        this.name = name;
        this.age = age;
        System.out.println("Student(String,name)");
    }
    private void eat(){
        System.out.println("i am eat");
    }
    public void sleep(){
        System.out.println("i am pig");
    }
    private void function(String str) {
        System.out.println(str);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    //查找重复元素
    public boolean containsNearbyDuplicate1(int[] array, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(map.containsKey(array[i]) && i - map.get(array[i]) <= k) {
                return true;
            }
            //它的val一直在被更新
            map.put(array[i],i);
        }
        return false;
    }
    public boolean containsNearbyDuplicate(int[] array, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
          if(map.containsKey(array[i]) && i - map.get(array[i]) <= k) {
              return true;
          }
          map.put(array[i],i);
        }
        return false;
    }
    //如果有两个一样的值就返回true
    public boolean containsDuplicate1(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x : nums) {
            if (!set.add(x)) {
                return true;
            }
        }
        return false;
    }

    //如果有两个一样的值就返回true
    public static boolean containsDuplicate(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int x:array) {
            map.put(x,map.getOrDefault(x,0)+1);
        }
        for (Map.Entry<Integer,Integer> x:map.entrySet()) {
            if(x.getValue() >= 2) {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        try {
            //通过名字找到该类
            Class<?> c1 = Class.forName("反射.Student");
            //通过类名.class获取到该类
            Class<?> c2 = Student.class;
            Student student = new Student();
            //通过类的对象获取到该类
            Class<?> c3 = student.getClass();
            System.out.println(c1 == c2);
            System.out.println(c2 == c3);
            System.out.println(c3);
            //结论，一个类只能由一个class对象
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
